package com.jnr.huatu.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.jnr.huatu.R;
import com.jnr.huatu.adapter.ListViewAdapter;
import com.jnr.huatu.bean.ListViewItem;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * 加载图片
 * @author：景辉
 * created at 2017/2/15 0015 上午 11:15
 */
public class ImgActivity extends AppCompatActivity {

    String imageUrl1 = "http://10.100.21.11/123/zq_test201702151118qqo.jpeg";
    private ImageView mImageView;

    private ListView mListView;
    private ImageLoader mImageLoader;
    private Context mContext;
    private String[] imageUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_img);
        initView();
    }
    public void initView() {
        mImageView = (ImageView) findViewById(R.id.image);
        imageUrl = new String[]{
                "http://10.100.21.11/123/zq_test201702151118qqo.jpeg",
                "http://10.100.21.11/123/zq_test201702151132bco.jpeg",
                "http://10.100.21.11/123/zq_test201702151132sks.jpeg",
                "http://10.100.21.11/123/1486791309192.png"
        };
        mContext=this;
        mImageLoader=ImageLoader.getInstance();
        mListView = (ListView) findViewById(R.id.listView);
    }
    /**
     * 显示一张图片
     * @author：景辉
     * created at 2017/2/15 0015 下午 2:46
     */
    public void xaizai(View view){
        //显示图片的配置
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.logo)//开始下载显示图片
                .showImageOnFail(R.drawable.jh)//下载错误显示图片
                .cacheInMemory(true)//设置图片是否缓存到内存中
                .cacheOnDisk(true)//设置图片是否缓存到SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片解码类型
                .build();
        ImageLoader.getInstance().displayImage(imageUrl1,mImageView, options);
    }
    /**
     * 多图显示
     * @author：景辉
     * created at 2017/2/15 0015 下午 2:46
     */
    public void duotu(View view){
        ArrayList<ListViewItem> arrayList = new ArrayList<>();
        ListViewItem listViewItem;
        for (int i = 0; i < imageUrl.length; i++) {
            listViewItem = new ListViewItem("This is " + i, imageUrl[i]);
            arrayList.add(listViewItem);
        }
        ListViewAdapter adapter = new ListViewAdapter(arrayList,mContext,mImageLoader);
        mListView.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImageLoader!=null) {
//            mImageLoader.clearMemoryCache();//清除内存缓存
//            mImageLoader.clearDiscCache();//清除SD卡缓存
        }
    }
}
